﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using BlueFinity.mvNET.CoreObjects;
using mvLibrary;

namespace fenk
{
    public partial class EditorProgramas : Form
    {
        public EditorProgramas()
        {
            InitializeComponent();
            this.cboOpenWith.SelectedIndex = 0;
        }

        private const string BUFFER_LOCATION = @"C:\Pick\EditorProgramas\{0}";

        private string GetFilePath(string fileName) { return string.Format(BUFFER_LOCATION, fileName);  }

        private string _errorMessage = string.Empty;
        private string _user;
        private mvAccount _pickAccount;
        public mvAccount PickAccount
        {
            get { return _pickAccount; }
            set { _pickAccount = value; }
        }
        public string User
        {
            get { return _user; }
            set { _user = value; }
        }

        public void ServerConnect(string operationType)
        {
            string itemId = txtItemId.Text;
            string fileName = txtPickFileName.Text;
            string programCode = this.OpenBufferFile(itemId);
            string item = programCode.Replace(DataBASIC.CRLF, DataBASIC.AM);
            _errorMessage = "";
            try
            {
                if (_pickAccount != null)
                {
                    _pickAccount.Login();
                    _pickAccount.CallProg("EDITOR.NET", ref operationType, ref fileName, ref itemId, ref item, ref _user,
                                          ref _errorMessage);
                    _pickAccount.Logout();

                }
            }
            catch (mvException mvEx)
            {
                _pickAccount.Logout();
                _errorMessage = mvEx.Message;
            }
            if (_errorMessage != "")
            {
                if (operationType != "C" & operationType != "B")
                {
                    MessageBox.Show("Error:\nMensaje de servidor: " + _errorMessage);
                }
            }
            string text = item.Replace(DataBASIC.AM, DataBASIC.CRLF);

            if (operationType.Equals("R"))
            {
                InitializeTextEditorProgram(text,itemId);
            }
        }

        #region Button Actions


        private void btnSave_Click(object sender, EventArgs e)
        {
            ServerConnect("W");
        }

        private void openButton_Click_1(object sender, EventArgs e)
        {
            if (ValidateRequiredFields())
            {
                ServerConnect("R");
            }
        }

        private void btnCompile_Click(object sender, EventArgs e)
        {
            if (ValidateRequiredFields())
            {
                ServerConnect("B");
                ShowCompileMessage();
            }
        }


        private void ShowCompileMessage()
        {
            MessageBox.Show(_errorMessage.Replace(DataBASIC.AM, DataBASIC.CRLF), "Resultado compilación", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        #endregion

        #region File and NotePad

        private string OpenBufferFile(string fileName)
        {
            try
            {
                return File.ReadAllText(GetFilePath(fileName));
            }
            catch (FileNotFoundException)
            {
                return string.Empty;
            }
        }

        private void InitializeTextEditorProgram(string text,string fileName)
        {
            var filePath = GetFilePath(fileName);
            File.WriteAllText(filePath, text);

            Process.Start(this.cboOpenWith.SelectedItem + ".exe", filePath);

        }

        #endregion

        #region File Watcher

        #endregion

        private bool ValidateRequiredFields()
        {
            bool isValid = true;
            if (string.IsNullOrEmpty(txtPickFileName.Text))
            {
                MessageBox.Show("Ingrese archivo (full path)", "Leer item");
                txtPickFileName.Focus();
                isValid = false;
            }
            if (string.IsNullOrEmpty(txtItemId.Text))
            {
                MessageBox.Show("Ingrese clave del registro(item id)", "Leer item");
                txtPickFileName.Focus();
                isValid = false;
            }

            return isValid;
        }

    }

}
